package one.stand.controller;

import com.github.pagehelper.PageHelper;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.CompanyCardRequest;
import com.jmp.base.dto.CompanyCardResponse;
import com.jmp.feign.CompanyFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.jmp.api.dto.CompanyCardUpdateRequest;
import com.jmp.api.dto.CompanyCardUsersResponse;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageRequest;
import one.stand.dto.PageResponse2;
import one.stand.model.BusinessCard;
import one.stand.model.ResultModel;
import one.stand.service.BusinessCardBaseService;
import one.stand.util.AssertUtil;
import one.stand.util.RequestContext;
import one.stand.util.RequestContextHolder;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author cjr
 */
@Api(description = "企业模块")
@RestController
@RequestMapping("crmApi/company/card")
public class CompanyCardController {
    @Autowired
    private CompanyFeign companyFeign;
    @Autowired
    private BusinessCardBaseService businessCardBaseService;

    @ApiOperation("人员列表")
    @PostMapping("/users")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<CompanyCardUsersResponse> users(@RequestAttribute PageRequest request) {
        final RequestContext.User user = RequestContextHolder.getRequestContext().getUser();
        final Integer companyId = user.getCompanyId();

        ResultModel<CompanyCardResponse> cardResponseResultModel = companyFeign.card(CompanyCardRequest.builder().companyId(companyId).build());
        AssertUtil.isTrue(cardResponseResultModel);
        final Integer companyCardId = cardResponseResultModel.getData().getCardId();

        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<BusinessCard> cardList = businessCardBaseService.getByCompanyId(companyId);
        PageResponse2<CompanyCardUsersResponse.User> users = ConvertUtils.page2(cardList, card -> CompanyCardUsersResponse.User.builder()
                .cardId(card.getCardId())
                .logo(card.getLogo())
                .name(card.getName())
                .selected(BooleanUtils.toInteger(companyCardId.equals(card.getCardId())))
                .build());

        CompanyCardUsersResponse response = CompanyCardUsersResponse.builder().users(users).build();
        return ResultModel.success(response);
    }

    @ApiOperation("更换名片")
    @PostMapping("/update")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel update(@RequestAttribute CompanyCardUpdateRequest request) {
        final RequestContext.User user = RequestContextHolder.getRequestContext().getUser();
        final Integer companyId = user.getCompanyId();
        ResultModel resultModel = companyFeign.cardUpdate(com.jmp.base.dto.CompanyCardUpdateRequest.builder()
                .companyId(companyId)
                .cardId(request.getCardId())
                .build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }
}
